package com.lanou.controller;

import com.lanou.entity.Province;
import com.lanou.entity.VipInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangxinleibobo
 */
@Controller
@RequestMapping(value = "/vip")
public class VipController {


    RestTemplate restTemplate=new RestTemplate();

    /**
     * 跳转到注册界面
     *
     * @return
     */
    @RequestMapping(value = "/startregister.do")
    public String startregister() {
        return "view/register";
    }
    /**
     * 接收注册提交的表单
     *
     * @return
     */
    @RequestMapping(value = "/addVip.do")
    @ResponseBody
    public Map<String, Object> addVip(@RequestParam Map<String, Object> params) {
        HashMap<String, Object> hashMap = new HashMap<>();
        System.out.println(params);
        hashMap.put("result", "success");
        return hashMap;
    }

    /**
     * 退出登录
     * @return
     */
    @RequestMapping(value = "/logout.do")
    public String logout() {
        return "view/login";
    }


    /**
     *
     * @return
     */
    @RequestMapping("/tofindPointRcd.do")
    public String tofindPointRcd(){
        return "view/findPointRcd";
    }


    /**
     * 根据会员id插入收货地址信息
     *
     * @return
     */
    @RequestMapping("/addAddrInfoByIdShowInfo.do")
    public ModelAndView addAddrInfoByIdShowInfo(@RequestParam Map<String,Object> map){
        //创建视图
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("view/messageAddress");

        //调用接口,将id传过去,得到vipInfo对象
        MultiValueMap multiValueMap=new LinkedMultiValueMap();
        multiValueMap.setAll(map);
        System.out.println("传过去的数据--"+multiValueMap);
        VipInfo vipInfo = restTemplate.postForObject("http://10.90.87.218:8080/port/getvipInfoById.do",multiValueMap,VipInfo.class);

        //获取省市信息,进行地址的添加,调用接口获取省市  "http://10.90.87.218:8080/port/getAllProvince.do"
        List<Province> provinceList = restTemplate.postForObject("http://10.90.87.218:8080/port/getAllProvince.do",null,List.class);
        System.out.println("--------"+provinceList);

        //将回显数据放入域中
        modelAndView.addObject("result",vipInfo);
        modelAndView.addObject("list",provinceList);
        return modelAndView;
    }

    /**
     *  根据省id信息查询市信息
     * @return
     */
    @RequestMapping("/findCity.do")
    @ResponseBody
    public List getCityByProvinceId(@RequestParam Map<String,Object> map){
        System.out.println("获取省id信息-----"+map);
        //根据省id获取市信息集合
        MultiValueMap multiValueMap=new LinkedMultiValueMap();
        multiValueMap.setAll(map);
        List list = restTemplate.postForObject("http://10.90.87.218:8080/port/getCity.do", multiValueMap, List.class);
        return list;
    }
}
